import sys

# 应用基础配置 BASE_DIR为Path类
from core.config import config, BASE_DIR 
print(f"当前项目的根目录： {BASE_DIR}")
sys.path.append(str(BASE_DIR))
# 访问服务器配置
print(f"服务器地址：{config.server.host}:{config.server.port}")

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.concurrency import asynccontextmanager
from routers.model_api import router as model_api_router
from db.client import db_client

@asynccontextmanager
async def lifespan(app: FastAPI):
    # 应用启动时连接数据库
    await db_client.connect() 
    yield
    # 应用关闭时断开连接
    await db_client.disconnect()  

app = FastAPI(lifespan=lifespan)

# 注册路由
app.include_router(model_api_router)

origins = [
    "http://vue-frontend",
    "http://vue-frontend:8080",
    "http://node-backend",
    "http://node-backend:8000",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


if __name__ == "__main__":
    import uvicorn
    uvicorn.run("main:app", host=config.server.host, port=config.server.port)
    